﻿using Grpc.Core;
using MagicOnion;
using MagicOnion.Client;
using MagicOnion.Server;
using RPC服务端.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace RPC客服端.Common
{
    /// <summary>
    /// 实现服务
    /// 可在实现的方法中添加自己的业务逻辑
    /// </summary>
    public class MyClient
    {
        public static async Task<int> ChenAsync(int x, int y)
        {
            try
            {
                var channel = new Channel("localhost", 1001, ChannelCredentials.Insecure);
                await channel.ConnectAsync();

                var client = MagicOnionClient.Create<IMyService>(channel);
                var result = await client.ChenAsync(x, y);
                await channel.ShutdownAsync();

                return await result;

            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public static async Task<int> ChuAsync(int x, int y)
        {
            try
            {
                var channel = new Channel("localhost", 1001, ChannelCredentials.Insecure);
                await channel.ConnectAsync();

                var client = MagicOnionClient.Create<IMyService>(channel);
                var result = await client.ChuAsync(x, y);

                await channel.ShutdownAsync();

                return await result;

            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public static async Task<int> JiaAsync(int x, int y)
        {
            try
            {
                var channel = new Channel("localhost", 1001, ChannelCredentials.Insecure);
                await channel.ConnectAsync();

                var client = MagicOnionClient.Create<IMyService>(channel);
                var result = await client.JiaAsync(x, y);

                await channel.ShutdownAsync();

                return await result;

            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public static async Task<int> JianAsync(int x, int y)
        {
            try
            {
                var channel = new Channel("localhost", 1001, ChannelCredentials.Insecure);
                await channel.ConnectAsync();

                var client = MagicOnionClient.Create<IMyService>(channel);
                var result = await client.JianAsync(x, y);

                await channel.ShutdownAsync();

                return await result;

            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}
